import type { RequiredDeep } from 'type-fest'

import { ajax } from '@/utils'

import type { components } from '../schema'
import type { IDeleteReq, IExportRes } from '../type'

/**
 * 配置接口
 */
export const configApi = {
  /**
   * 获取配置分页
   */
  getConfigPage(data: IGetConfigPageReq): Promise<IGetConfigPageRes> {
    return ajax.post('/system/config/page', data)
  },
  /**
   * 获取配置值
   */
  getConfigValue(key: string): Promise<IGetConfigValueRes> {
    return ajax.get('/system/config/value', { params: { key } })
  },
  /**
   * 添加配置
   */
  createConfig(data: ICreateConfigReq) {
    return ajax.post('/system/config/create', data)
  },
  /**
   * 更新配置
   */
  updateConfig(id: string, data: IUpdateConfigReq) {
    return ajax.put('/system/config/update', data, { params: { id } })
  },
  /**
   * 删除配置
   */
  deleteConfig(data: IDeleteReq) {
    return ajax.delete('/system/config/delete', { data })
  },
  /**
   * 导出配置分页
   */
  exportConfigPage(data: IGetConfigPageReq): Promise<IExportRes> {
    return ajax.post('/system/config/export', data)
  }
}

export type IGetConfigPageReq = components['schemas']['FindConfigPageDTO']
export type IGetConfigPageRes = RequiredDeep<
  components['schemas']['FindConfigPageVO']['data']
>
export type IConfigPageItem = IGetConfigPageRes['list'][0]
export type IGetConfigValueRes = RequiredDeep<
  components['schemas']['GetConfigValueVO']['data']
>
export type ICreateConfigReq = components['schemas']['CreateConfigDTO']
export type IUpdateConfigReq = components['schemas']['UpdateConfigDTO']
